<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta name="referrer" content="no-referrer"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>04.Python判断语句 | 王扣笔记</title><meta name="author" content="王扣"><meta name="copyright" content="王扣"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Python入门课程  判断语句   章节目录 [!WARNING|label:本章需要学习的内容]   布尔类型和比较运算符 if语句的基本格式 if else 语句 if elif else语句 判断语句的嵌套 实战案例   布尔类型 比较运算符 if语句的基本格式  练习题代码 : 成年人判断  12345print(f&quot;欢迎来到黑马儿童游乐场，儿童免费，成人收费&quot;)ag">
<meta property="og:type" content="article">
<meta property="og:title" content="04.Python判断语句">
<meta property="og:url" content="http://grace.666443.xyz/posts/9b6113bf.html">
<meta property="og:site_name" content="王扣笔记">
<meta property="og:description" content="Python入门课程  判断语句   章节目录 [!WARNING|label:本章需要学习的内容]   布尔类型和比较运算符 if语句的基本格式 if else 语句 if elif else语句 判断语句的嵌套 实战案例   布尔类型 比较运算符 if语句的基本格式  练习题代码 : 成年人判断  12345print(f&quot;欢迎来到黑马儿童游乐场，儿童免费，成人收费&quot;)ag">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://p.ananas.chaoxing.com/star3/origin/f477c264e6f1e4c9138450a3a0359907.png">
<meta property="article:published_time" content="2022-12-08T11:37:42.000Z">
<meta property="article:modified_time" content="2022-12-08T12:09:06.589Z">
<meta property="article:author" content="王扣">
<meta property="article:tag" content="笔记 开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://p.ananas.chaoxing.com/star3/origin/f477c264e6f1e4c9138450a3a0359907.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://grace.666443.xyz/posts/9b6113bf"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '04.Python判断语句',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-12-08 20:09:06'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://p.ananas.chaoxing.com/star3/origin/f477c264e6f1e4c9138450a3a0359907.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">王扣笔记</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">04.Python判断语句</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-12-08T11:37:42.000Z" title="发表于 2022-12-08 19:37:42">2022-12-08</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-12-08T12:09:06.589Z" title="更新于 2022-12-08 20:09:06">2022-12-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python%E5%9F%BA%E7%A1%80/">python基础</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="04.Python判断语句"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Python入门课程-判断语句"><a href="#Python入门课程-判断语句" class="headerlink" title="Python入门课程  判断语句"></a>Python入门课程  判断语句</h1><hr>
<p><img src="https://img-blog.csdnimg.cn/e1f4c2c5922442f9862fdcfb9480493b.png" alt="1.jpg"></p>
<hr>
<h2 id="章节目录"><a href="#章节目录" class="headerlink" title="章节目录"></a><strong>章节目录</strong></h2><blockquote>
<p>[!WARNING|label:本章需要学习的内容]</p>
</blockquote>
<ul>
<li>布尔类型和比较运算符</li>
<li>if语句的基本格式</li>
<li>if else 语句</li>
<li>if elif else语句</li>
<li>判断语句的嵌套</li>
<li>实战案例</li>
</ul>
<hr>
<h2 id="布尔类型"><a href="#布尔类型" class="headerlink" title="布尔类型"></a><strong>布尔类型</strong></h2><p><img src="https://img-blog.csdnimg.cn/5161d5f93d49480c916277d57a0221ef.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/eb9a29e70ee848648f7206a0421cde2d.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/f524353187004c19810e806edb54dfd4.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/aed7cb6f93fa4d91ab3d258e19ea6922.png" alt="5.jpg"></p>
<h2 id="比较运算符"><a href="#比较运算符" class="headerlink" title="比较运算符"></a><strong>比较运算符</strong></h2><p><img src="https://img-blog.csdnimg.cn/04fefb2e0b8d41d88fbb735ee244b16d.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/f3b1c0afa06142cd82b66e15d86d2361.png" alt="7.jpg"><br><img src="https://img-blog.csdnimg.cn/a657147e951140f9a5ad91ce49a9a115.png" alt="8.jpg"></p>
<h2 id="if语句的基本格式"><a href="#if语句的基本格式" class="headerlink" title="if语句的基本格式"></a><strong>if语句的基本格式</strong></h2><p><img src="https://img-blog.csdnimg.cn/ae8fc66fbc114c029d55753c7edf94ef.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/ea708cf338364232bafd308ef582b202.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/28a70dfe70c04cdd9b025e8b22f2784f.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/11161f1f7c0943d49842114125abab08.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/3ad30d59a9964f5baef27d291e83fbcd.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/0facd458808b4a30b108322bc9cf7a8b.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/31b431c38717417abf64cdfd9f48e1ad.png" alt="7.jpg"></p>
<blockquote>
<p>练习题代码 : 成年人判断</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;欢迎来到黑马儿童游乐场，儿童免费，成人收费&quot;</span>)</span><br><span class="line">age = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入您的年龄:&quot;</span>))</span><br><span class="line"><span class="keyword">if</span> age &gt;= <span class="number">18</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;您已经成年，游玩需要补票10元&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;祝您游玩愉快！&quot;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="if-else-语句"><a href="#if-else-语句" class="headerlink" title="if else 语句"></a><strong>if else 语句</strong></h2><p><img src="https://img-blog.csdnimg.cn/c00a433bdf2e4e119cbc29652e0823ff.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/21a4769137014acca939f1a945d7177d.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/39df20827136438b91d95bd0eecb5aec.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/49008d4b711045ad89c0bcac13ace6e0.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/0a211c31ba0b455ba47365034459b485.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/9167623a15bd45df9bcd69a89d75393b.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/dbaafe4e61b94b1290740bbcdbc7246a.png" alt="7.jpg"></p>
<blockquote>
<p>练习题代码： 我要买票吗</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">&quot;欢迎来到黑马动物园&quot;</span>)</span><br><span class="line">height = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入您的身高(cm):&quot;</span>))</span><br><span class="line"><span class="keyword">if</span> height &gt; <span class="number">120</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;您的身高<span class="subst">&#123;height&#125;</span>cm，已经超出120cm,游玩需要购票：10元&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;您的身高<span class="subst">&#123;height&#125;</span>cm，未超出120cm,可以免费游玩&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;祝您游玩愉快。&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="if-elif-else语句"><a href="#if-elif-else语句" class="headerlink" title="if elif else语句"></a><strong>if elif else语句</strong></h2><p><img src="https://img-blog.csdnimg.cn/30199db9a9bc4e069e5eeb923bedec4f.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/d7f5369767c840fc9eab8e50ee96d9f2.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/f3833e8017744abbaf5cd4ca5ef41463.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/a30af0274fb54d2aa7c7df065006aa72.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/fe8d12d4db1b4919a23c02a992e9a1d8.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/d15f241b44274262b63d72afac655c6e.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/0d8762bf0a4848f39e589f3f5923da72.png" alt="7.jpg"><br><img src="https://img-blog.csdnimg.cn/c43e98f57cd34430b08a3af611ac9fa6.png" alt="8.jpg"><br><img src="https://img-blog.csdnimg.cn/3e72e54b204e42d2a4de1f574f54ab51.png" alt="9.jpg"><br><img src="https://img-blog.csdnimg.cn/4ce5fe36d3c243b080de8f45acc54fd5.png" alt="10.jpg"></p>
<blockquote>
<p>练习题代码：猜猜心里数字</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个变量 数字类型</span></span><br><span class="line">num = <span class="number">10</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 输入猜测的数字</span></span><br><span class="line"><span class="keyword">if</span> <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入第一次猜想的数字：&quot;</span>)) == num:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;第1次就猜对了&quot;</span>)</span><br><span class="line"><span class="keyword">elif</span> <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;不对，再猜一次:&quot;</span>)) == num:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;第2次才对了&quot;</span>)</span><br><span class="line"><span class="keyword">elif</span> <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;最后一次机会：&quot;</span>)) == num:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;最后终于猜对了&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">f&quot;sorry,全部猜测错了，我心里想的数字是：<span class="subst">&#123;num&#125;</span>&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="判断语句的嵌套"><a href="#判断语句的嵌套" class="headerlink" title="判断语句的嵌套"></a><strong>判断语句的嵌套</strong></h2><p><img src="https://img-blog.csdnimg.cn/d25477133b8641d08c7673b543a402a1.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/2d01b3d839454bb1a8ad425c5b74a3b1.png" alt="2.jpg"><br><img src="https://img-blog.csdnimg.cn/37208fa3222e4edc8dc14ce54cd04cb6.png" alt="3.jpg"><br><img src="https://img-blog.csdnimg.cn/f6ca1edb689f4a3c94217824eb086675.png" alt="4.jpg"><br><img src="https://img-blog.csdnimg.cn/594dc8bc820c4a10ac4204f7fbd7ef06.png" alt="5.jpg"><br><img src="https://img-blog.csdnimg.cn/ad400391e6c84e658c774e1031b385db.png" alt="6.jpg"><br><img src="https://img-blog.csdnimg.cn/8c452cd6b8b1490ebc0e4951ad85d6f6.png" alt="7.jpg"></p>
<h2 id="实战案例"><a href="#实战案例" class="headerlink" title="实战案例"></a><strong>实战案例</strong></h2><p><img src="https://img-blog.csdnimg.cn/0ff3c9d6992549ae8ebd1a1748552970.png" alt="1.jpg"><br><img src="https://img-blog.csdnimg.cn/f05cfd9bbb714f2bb9579e6423554061.png" alt="2.jpg"></p>
<blockquote>
<p>实战案例代码</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个数字，随机产生 1-10</span></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line">num = random.randint(<span class="number">1</span>,<span class="number">10</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">f&quot;作弊外挂友情提示：将要猜测的数字为：<span class="subst">&#123;num&#125;</span>&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取输入的数字</span></span><br><span class="line">guess_num = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请输入您要猜的数字:&quot;</span>))</span><br><span class="line"><span class="comment"># 三次猜测</span></span><br><span class="line"><span class="keyword">if</span> guess_num == num:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;真厉害，第一次就猜对了&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="keyword">if</span> guess_num &gt; num:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;猜大了&quot;</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;猜小了&quot;</span>)</span><br><span class="line">    guess_num = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;第二次猜测，请输入您要猜的数字:&quot;</span>))</span><br><span class="line">    <span class="keyword">if</span> guess_num == num:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;恭喜：第二次猜对了&quot;</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">if</span> guess_num &gt; num:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;第二次猜测，猜大了&quot;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;第二次猜测，猜小了&quot;</span>)</span><br><span class="line">        guess_num = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;第3次猜测，请输入您要猜的数字:&quot;</span>))</span><br><span class="line">        <span class="keyword">if</span> guess_num == num:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;恭喜，第三次猜中&quot;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;三次机会用完，sorry！&quot;</span>)</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://grace.666443.xyz">王扣</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://grace.666443.xyz/posts/9b6113bf.html">http://grace.666443.xyz/posts/9b6113bf.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://grace.666443.xyz" target="_blank">王扣笔记</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://p.ananas.chaoxing.com/star3/origin/f477c264e6f1e4c9138450a3a0359907.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/faab6e1.html"><img class="prev-cover" src="https://p.ananas.chaoxing.com/star3/origin/0df4f1a3cf2f76812e9cbf25c3190113.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">05.Python循环语句</div></div></a></div><div class="next-post pull-right"><a href="/posts/a5fa344e.html"><img class="next-cover" src="https://p.ananas.chaoxing.com/star3/origin/ce756cc85425d56cab1a5e7cf7934af0.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">03.Python基础语法</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">王扣</div><div class="author-info__description">常用开发中的笔记记录</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">1</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog 技术笔记</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Python%E5%85%A5%E9%97%A8%E8%AF%BE%E7%A8%8B-%E5%88%A4%E6%96%AD%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.</span> <span class="toc-text">Python入门课程  判断语句</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AB%A0%E8%8A%82%E7%9B%AE%E5%BD%95"><span class="toc-number">1.1.</span> <span class="toc-text">章节目录</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%83%E5%B0%94%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.2.</span> <span class="toc-text">布尔类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AF%94%E8%BE%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.3.</span> <span class="toc-text">比较运算符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#if%E8%AF%AD%E5%8F%A5%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A0%BC%E5%BC%8F"><span class="toc-number">1.4.</span> <span class="toc-text">if语句的基本格式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#if-else-%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.5.</span> <span class="toc-text">if else 语句</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#if-elif-else%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.6.</span> <span class="toc-text">if elif else语句</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E8%AF%AD%E5%8F%A5%E7%9A%84%E5%B5%8C%E5%A5%97"><span class="toc-number">1.7.</span> <span class="toc-text">判断语句的嵌套</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9E%E6%88%98%E6%A1%88%E4%BE%8B"><span class="toc-number">1.8.</span> <span class="toc-text">实战案例</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/ae1a0acd.html" title="18.Python爬虫-requests模块"><img src="https://p.ananas.chaoxing.com/star3/origin/b230c5f9060b1f422a86e332d8e37926.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="18.Python爬虫-requests模块"/></a><div class="content"><a class="title" href="/posts/ae1a0acd.html" title="18.Python爬虫-requests模块">18.Python爬虫-requests模块</a><time datetime="2022-12-08T12:53:30.000Z" title="发表于 2022-12-08 20:53:30">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/ed7a56b2.html" title="17.Python爬虫-数据解析与提取"><img src="https://p.ananas.chaoxing.com/star3/origin/8f2179d04bfe0e48cc706ae112c97952.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="17.Python爬虫-数据解析与提取"/></a><div class="content"><a class="title" href="/posts/ed7a56b2.html" title="17.Python爬虫-数据解析与提取">17.Python爬虫-数据解析与提取</a><time datetime="2022-12-08T12:52:55.000Z" title="发表于 2022-12-08 20:52:55">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/5038ed.html" title="16.Python爬虫-基础"><img src="https://p.ananas.chaoxing.com/star3/origin/2054c03c91d338203c730610ead1b303.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="16.Python爬虫-基础"/></a><div class="content"><a class="title" href="/posts/5038ed.html" title="16.Python爬虫-基础">16.Python爬虫-基础</a><time datetime="2022-12-08T12:52:11.000Z" title="发表于 2022-12-08 20:52:11">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/2b5a0751.html" title="15.Python之Mysql数据库相关"><img src="https://p.ananas.chaoxing.com/star3/origin/2a1821704670b5cf8c2289255e183d62.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="15.Python之Mysql数据库相关"/></a><div class="content"><a class="title" href="/posts/2b5a0751.html" title="15.Python之Mysql数据库相关">15.Python之Mysql数据库相关</a><time datetime="2022-12-08T12:17:02.000Z" title="发表于 2022-12-08 20:17:02">2022-12-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/6ebaa02b.html" title="14.Python面向对象"><img src="https://p.ananas.chaoxing.com/star3/origin/d24d9bfcd4ce3f910d6b5cdc78676d75.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="14.Python面向对象"/></a><div class="content"><a class="title" href="/posts/6ebaa02b.html" title="14.Python面向对象">14.Python面向对象</a><time datetime="2022-12-08T12:10:47.000Z" title="发表于 2022-12-08 20:10:47">2022-12-08</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 王扣</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><div class="fb-customerchat" id="fb-customer-chat" attribution="biz_inbox"></div><script>document.getElementById('fb-root') ? '' : document.body.insertAdjacentHTML('afterend', '<div id="fb-root"></div>')

window.fbAsyncInit = function() {
  FB.init({
    xfbml: true,
    version: 'v15.0'
  });
};

(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = 'https://connect.facebook.net/zh_CN/sdk/xfbml.customerchat.js';
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

if (false) {
  var chatBtnFn = () => {
    var chatBtn = document.getElementById("chat_btn")
    chatBtn.addEventListener("click", function(){
      FB.CustomerChat.show();
    });
  }
  chatBtnFn()
} else {
  if (false) {
    function chatBtnHide () {
      FB.CustomerChat.hide()
    }
    function chatBtnShow () {
      FB.CustomerChat.show(false)
    }
  }
}</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>